package com.poor.user.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.poor.core.Result;
import com.poor.core.ResultGenerator;
import com.poor.user.client.configurer.UrlConfigurer;
import com.poor.user.client.model.request.UserRequestBean;
import com.poor.user.client.service.UserService;
import com.poor.utils.RestTemplateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    private RestTemplate restTemplate;

    @Override
    public void findUserAll() {

    }

    @Override
    public void registerUser(UserRequestBean userRequestBean) {
        if (StringUtils.isEmpty(userRequestBean.getUserPassWord())) {
            userRequestBean.setUserPassWord("");
        }
        RestTemplateUtils.postForJson(UrlConfigurer.BASE_USER_TEST_URL + "/add", JSONObject.toJSONString(userRequestBean));
    }

    @Override
    public List<UserRequestBean> findByWxUnionId(UserRequestBean userRequestBean) {
        String result = RestTemplateUtils.postForJson(UrlConfigurer.BASE_USER_TEST_URL + "/findByWxUnionId", JSONObject.toJSONString(userRequestBean));
        JSONObject jsonObject = JSON.parseObject(result);
        return JSONArray.parseArray(jsonObject.getString("data"), UserRequestBean.class);
    }

    @Override
    public void updateUser(UserRequestBean userRequestBean) {
        RestTemplateUtils.postForJson(UrlConfigurer.BASE_USER_TEST_URL + "/update", JSONObject.toJSONString(userRequestBean));
    }
}
